package com.smartEdu.module.schedule;

public class EncodeToLogin {
	public static boolean isNan(int t) {
		if(t == 0) return true;
		return !String.valueOf(t).matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }
	
	public static char getOneChar(char arr[],int idx){
		if(idx>=arr.length){
			return 0;
		}
		else return arr[idx];
	}
	public static String getEncodeString(String clearConntent){
		String keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		char[] charArray=clearConntent.toCharArray();
		String output = "";
		char chr1, chr2, chr3;
		int enc1, enc2, enc3, enc4;
		int i = 0;
		do {
			chr1 = getOneChar(charArray,i++);
			chr2 = getOneChar(charArray,i++);
			chr3 = getOneChar(charArray,i++);
			enc1 = (chr1 >> 2);
			enc2 = (((chr1 & 3) << 4) | (chr2 >> 4));
			enc3 = (((chr2 & 15) << 2) | (chr3 >> 6));
			enc4 = (chr3 & 63);
			if (isNan((int)chr2)) {
				enc3 = enc4 = 64;
			} 
			else if (isNan((int)chr3)) {
				enc4 = 64;
			}
		    output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
		    chr1 = chr2 = chr3 = 0;
		    enc1 = enc2 = enc3 = enc4 = 0;
		} while ( i < clearConntent.length() );
		return output;
	}
	
	public static void main(String[] args){
		System.err.println(getEncodeString("140202021019")+"%%%"+getEncodeString("5201314LIANRU"));
//		System.err.println(getEncodeString("130201031035")+"%%%"+getEncodeString("abc123abc"));
	}
}
